package com.acheli.registry.network.packet;

import com.acheli.ACHeli;
import com.acheli.registry.config.CustomDataBus;
import com.acheli.registry.network.ACHMessages;
import com.acheli.rideable.functionLathes.component.DefaultComponentFactoryRegistry;
import com.acheli.rideable.functionLathes.component.DefaultComponentProvider;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraftforge.network.NetworkDirection;
import net.minecraftforge.network.NetworkEvent;
import net.minecraftforge.network.simple.SimpleChannel;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.function.Supplier;

public class GsonComponentFLRegistryC2SPacket {
    // 添加字符串字段
    private final CompoundTag tag;

    // 从字节缓冲区解码
    public GsonComponentFLRegistryC2SPacket(FriendlyByteBuf buf) {
        this.tag = buf.readNbt();
    }


    public GsonComponentFLRegistryC2SPacket(String PACKNAME, String componentName, String jsonString) {
        tag = new CompoundTag();
        tag.putString("PACKNAME", PACKNAME);
        tag.putString("componentName", componentName);
        tag.putString("jsonString", jsonString);
    }

    // 编码到字节缓冲区
    public void toBytes(FriendlyByteBuf buf) {
        buf.writeNbt(tag);
    }

    private boolean handle(Supplier<NetworkEvent.Context> supplier) {
        String PACKNAME = tag.getString("PACKNAME");
        String componentName = tag.getString("componentName");
        String jsonString = tag.getString("jsonString");
        ACHeli.LOGGER.info("GsonComponentFLRegistryC2SPacket => PACKNAME: " + PACKNAME);
        NetworkEvent.Context context = supplier.get();
        context.enqueueWork(() -> {
            DefaultComponentProvider provider = DefaultComponentFactoryRegistry
                    .registerForGsonDefaultComponentProvider(PACKNAME, componentName, jsonString);
            if (provider != null) {
                if (! CustomDataBus.gsonDefaultComponentProvidersMap.containsKey(PACKNAME)) {
                    CustomDataBus.gsonDefaultComponentProvidersMap.put(PACKNAME, new ArrayList<>());
                }
                CustomDataBus.gsonDefaultComponentProvidersMap.get(PACKNAME).add(provider);
            }

        });
        return true;
    }

    public static void register(SimpleChannel channel) {
        channel.messageBuilder(GsonComponentFLRegistryC2SPacket.class, ACHMessages.id(), NetworkDirection.PLAY_TO_SERVER)
                .decoder(GsonComponentFLRegistryC2SPacket::new)
                .encoder(GsonComponentFLRegistryC2SPacket::toBytes)
                .consumerMainThread(GsonComponentFLRegistryC2SPacket::handle)
                .add();
    }

    private static byte[] mapToBytes(HashMap<String, String> map) throws IOException {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try (ObjectOutputStream oos = new ObjectOutputStream(bos)) {
            oos.writeObject(map);
        }
        return bos.toByteArray();
    }

    private static HashMap<String, String> bytesToMap(byte[] bytes) throws IOException, ClassNotFoundException {
        try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bytes))) {
            return (HashMap<String, String>) ois.readObject();
        }
    }
}